package com.xcity.db.entity;

import java.util.Date;

import com.xcity.game.mail.MailAttach;

import naga.x.game.time.TimeUpdater;

public class Mail {

	private int id;
	private byte type;
	private long srcId;
	private long destId;
	private String title;
	private String content;
	private MailAttach attachment;
	private Date postTime;
	private Date expireTime;
	private byte state;
	
	public static final byte TYPE_NORMAL = 0; // 普通邮件(玩家之间)
	public static final byte TYPE_SYSTEM = 1; // 系统邮件
	public static final byte TYPE_FINANICAL = 2; // 理财邮件
	public static final byte TYPE_PVP_MSG = 3; // pvp战斗消息
	
	public static final byte STATE_UNREAD = 0; // 未读
	public static final byte STATE_READ = 1; // 已读
	public static final byte STATE_RECEIVED = 2; // 附件已领取
	public static final byte STATE_HIDE = 3; // 隐藏
	
	public static final long DEFAULT_VALID_TIME = 3 * 24 * 60 * 60 * 1000L; // 邮件默认有效期3天
	public static final long REWARD_MAIL_VALID_TIME = 10 * 24 * 60 * 60 * 1000L; // 带附件邮件默认有效期10天
	
	public Mail() {}
	
	public Mail(Mail ref) {
		this(ref.type, ref.srcId, ref.destId,
				ref.title, ref.content, ref.attachment,
				ref.postTime, ref.expireTime, ref.state);
	}
	
	public Mail(byte type, long srcId, long destId, String title, String content) {
		this(type, srcId, destId, title, content, null);
	}
	
	public Mail(byte type, long srcId, long destId, String title, String content, MailAttach attachment) {
		this(type, srcId, destId, 
				title, content, attachment,
				new Date(TimeUpdater.getInstance().now()), 
				new Date(obtainExpireTime(attachment)),
				STATE_UNREAD);
	}
	
	public Mail(byte type, long srcId, long destId,
			String title, String content, MailAttach attachment,
			Date postTime, Date expireTime, byte state) {
		this.type = type;
		this.srcId = srcId;
		this.destId = destId;
		this.title = title;
		this.content = content;
		this.attachment = attachment;
		this.postTime = postTime;
		this.expireTime = expireTime;
		this.state = state;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public byte getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = type;
	}

	public long getSrcId() {
		return srcId;
	}

	public void setSrcId(long srcId) {
		this.srcId = srcId;
	}

	public long getDestId() {
		return destId;
	}

	public void setDestId(long destId) {
		this.destId = destId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public MailAttach getAttachment() {
		return attachment;
	}

	public void setAttachment(MailAttach attachment) {
		this.attachment = attachment;
	}

	public Date getPostTime() {
		return postTime;
	}

	public void setPostTime(Date postTime) {
		this.postTime = postTime;
	}

	public Date getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(Date expireTime) {
		this.expireTime = expireTime;
	}

	public byte getState() {
		return state;
	}

	public void setState(byte state) {
		this.state = state;
	}
	
	private static long obtainExpireTime(MailAttach attach) {
		return TimeUpdater.getInstance().now() + (attach != null ? REWARD_MAIL_VALID_TIME : DEFAULT_VALID_TIME);
	}
	
}
